
package LwJGL;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

/**
 * TimerExample.java
 * Created on May 3, 2014
 * Author Joe Porter
 */
public class TimerExample {
    
    
    
    long lastFrame;
    
    int fps;
    
    DisplayMode displayMode = null;
    
        
    long lastFPS;
    
    Quad quad = new Quad(400f,300f,50f, 0);
    Pyramid p = new Pyramid();
    Cube c = new Cube();
    
    public void start(){
        try{
            DisplayMode[] modes = Display.getAvailableDisplayModes();
            for (int i = 0; i < modes.length; i++)
         {
             if (modes[i].getWidth() == 800
             && modes[i].getHeight() == 600
             && modes[i].isFullscreenCapable())
               {
                    displayMode = modes[i];
               }
         }
            Display.setDisplayMode(displayMode);
            //Display.setFullscreen(true);
            Display.create();
        } catch(LWJGLException e){
            e.printStackTrace();
            System.exit(0);
        }
        initGL();
        getDelta();
        lastFPS = getTime();
        
        while(!Display.isCloseRequested()){
            int delta = getDelta();
            
            update(delta);
            
            //renderGL();
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
            //quad.update();
            //p.renderGl();
            c.renderGL();
            Display.update();
            Display.sync(60);
        }
    }
    
    public void update(int delta){
        
        //Quad # 1
        if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)){ quad.x -= .5f *delta;}
        if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){ quad.x += .5f *delta;}
        
        if(Keyboard.isKeyDown(Keyboard.KEY_UP)){ quad.y += .5f *delta;}
        if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)){ quad.y -= .5f *delta;}
        
        if(Keyboard.isKeyDown(Keyboard.KEY_LCONTROL)){quad.rotation += .1f*delta;}
        if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){quad.rotation -= .1f*delta;}
        
        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){System.exit(1);}
        
        if(quad.x-quad.size<0) {quad.x=quad.size;}
        if(quad.x+quad.size>displayMode.getWidth()) {quad.x=displayMode.getWidth()-quad.size;}
        if(quad.y-quad.size<0) {quad.y=quad.size;}
        if(quad.y+quad.size>displayMode.getHeight()) {quad.y=displayMode.getHeight()-quad.size;}
        
        if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)){ GL11.glTranslatef(-.1f, 0, 0);}
        if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)){ GL11.glTranslatef(.1f, 0, 0);}
        
        if(Keyboard.isKeyDown(Keyboard.KEY_UP)){ GL11.glTranslatef(0,.1f, 0);}
        if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)){ GL11.glTranslatef(0, -.1f, 0);}
    
        if(Keyboard.isKeyDown(Keyboard.KEY_LCONTROL)){GL11.glTranslatef(0, 0, .1f);}
        if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){GL11.glTranslatef(0, 0, -.1f);}
    }
    public int getDelta(){
        long time = getTime();
        int delta = (int) (time - lastFrame);
        lastFrame = time;
        
        return delta;
    }
    
    public long getTime(){
        return (Sys.getTime() * 1000 / Sys.getTimerResolution());
    }
    
    public void updateFPS(){
        if ( getTime() - lastFPS > 1000){
            Display.setTitle("FPS: " + fps);
            fps = 0;
            lastFPS +=1000;
        }
        fps++;
    }
    
    public void initGL(){
        //GL11.glMatrixMode(GL11.GL_PROJECTION);
        //GL11.glLoadIdentity();
        //GL11.glOrtho(0, 800, 0, 600, 1, -1);
        //GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
        GL11.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
        GL11.glClearDepth(1.0); // Depth Buffer Setup
        GL11.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
        GL11.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do

        GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
        GL11.glLoadIdentity(); // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(
          45.0f,
          (float) displayMode.getWidth() / (float) displayMode.getHeight(),
          0.1f,
          100.0f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
        
        // Really Nice Perspective Calculations
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    }
    
    
    /*public void renderGL(){
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        
        GL11.glColor3f(.5f, .5f, 1.0f);
        
        GL11.glPushMatrix();
            GL11.glTranslatef(x, y, 0);
            GL11.glRotatef(rotation, 0f, 0f, 1f);
            GL11.glTranslatef(-x, -y, 0);
            
            GL11.glBegin(GL11.GL_QUADS);
                GL11.glVertex2f(x-50, y-50);
                GL11.glVertex2f(x+50, y-50);
                GL11.glVertex2f(x+50, y+50);
                GL11.glVertex2f(x-50, y+50);
            GL11.glEnd();
        GL11.glPopMatrix();
                
    }
    */
    public static void main(String[] args){
        TimerExample te = new TimerExample();
        te.start();
    }
}
